package cn.newshasha.hlx.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;

/** 自定义微信测试号工具类
 * @author bSu
 * @date 2021/1/31 - 18:02
 */
@Component
public class WeChatUtils {

    @Value("${weChat.appID}")
    private String appId;

    @Value("${weChat.appsecret}")
    private String appsecret;

    @Value("${weChat.accTokenUrl}")
    private String accTokenUrl;

    @Value("${weChat.sendTemplateUrl}")
    private String sendTemplateUrl;

    private String access_token;

    private static Logger logger = Logger.getLogger(HlxUtils.class);

    private static String getName(){
        return HlxUtils.class.getName();
    }

    //发送模板信息.0-success 1-fail
    public int sendTemplate() throws IOException {
        //获取access_token
        this.access_token =getAccess_token();
        if(StringUtil.isBlank(getAccess_token())){
            return 1;
        }
        String str="{\n" +
                "           \"touser\":\"o3c3cwaeKnXPyt21bBCZR9HdSsHM\",\n" +
                "           \"template_id\":\"t10RUjKQO2z4rpASbWPMfoDnsxRzYKH_jJLFtwCClr0\",     \n" +
                "           \"url\":\"https://www.baidu.com/s?ie=UTF-8&wd=%E5%89%91%E6%9D%A5\",   \n" +
                "           \"data\":{\n" +
                "                   \"first\": {\n" +
                "                       \"value\":\"恭喜你，小说更新啦！\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword1\":{\n" +
                "                       \"value\":\"《剑来》\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword2\": {\n" +
                "                       \"value\":\"第八百五十三章 猜错的谜底\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"remark\":{\n" +
                "                       \"value\":\"点击详情即可查看\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   }\n" +
                "           }\n" +
                "       }";
        //发信
        Connection.Response post = HttpUtils.post(getSendTemplateUrl() + this.access_token, str);
        System.out.println(post.body());
        return 0;
    }



    public WeChatUtils() {
    }

    public WeChatUtils(String access_token) {
        this.access_token = access_token;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public void setAppsecret(String appsecret) {
        this.appsecret = appsecret;
    }

    public void setAccTokenUrl(String accTokenUrl) {
        this.accTokenUrl = accTokenUrl;
    }

    public void setAccess_token(String access_token) {
        this.access_token = access_token;
    }

    public String getAppId() {
        return appId;
    }

    public String getAppsecret() {
        return appsecret;
    }

    public String getAccTokenUrl() {
        return accTokenUrl;
    }

    public String getAccess_token() {
        try {
            logger.info(getName()+",获取access_token开始");
            Connection.Response response = HttpUtils.get(getAccTokenUrl());
            JSONObject json = JSONObject.parseObject(response.body());
            String access_token = (String) json.get("access_token");
            logger.info(getName()+",获取access_token结束【"+access_token+"】");
            return access_token;
        }catch (Exception e){
            logger.error(getName()+"，获取access_token失败",e);
            return null;
        }
    }

    public String getSendTemplateUrl() {
        return sendTemplateUrl;
    }

    public void setSendTemplateUrl(String sendTemplateUrl) {
        this.sendTemplateUrl = sendTemplateUrl;
    }
}
